package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.entity.greendao.UserMsgDaoHelper;
import com.lanou.horo.zhuizhui.entity.greendao.User_Message;

/**
 * Created by dllo on 15/11/7.
 */
public class RegisteredActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener, TextWatcher {
    private EditText passwordET, nameET;
    private ImageView lookimage, registeredBackBtn, noLookimage;
    private TextView passwordSizeTV, nameSizeTv;
    private Button registeredBtn;
    private UserMsgDaoHelper daoHelper;

    private CharSequence temp;//监听前的文本
    private int editStart;//光标开始位置
    private int editEnd;//光标结束位置
    private final int charMaxNum = 15;
    private String  rname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        passwordSizeTV = (TextView) findViewById(R.id.passwordSizeTV);
        nameSizeTv = (TextView) findViewById(R.id.nameSizeTV);
        registeredBtn = (Button) findViewById(R.id.registeredBtn);
        registeredBtn.setOnClickListener(this);

        passwordET = (EditText) findViewById(R.id.passwordET);
        passwordET.setOnFocusChangeListener(this);
        nameET = (EditText) findViewById(R.id.nameET);
        nameET.setOnFocusChangeListener(this);
        nameET.addTextChangedListener(this);
        lookimage = (ImageView) findViewById(R.id.lookimage);
        noLookimage = (ImageView) findViewById(R.id.noLookimage);
        registeredBackBtn = (ImageView) findViewById(R.id.registeredBackBtn);
        registeredBackBtn.setOnClickListener(this);

        daoHelper = UserMsgDaoHelper.getInstance();
//        password = passwordET.getText().toString();
        rname = nameET.getText().toString();
        if (rname.equals("")) {
            registeredBtn.setBackgroundColor(0xffcecece);
            registeredBtn.setClickable(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.registeredBackBtn:
                finish();
                break;
            case R.id.registeredBtn:
               String password = passwordET.getText().toString();
                String name = nameET.getText().toString();
                if ( password.equals("")) {
                    showCustomToast("密码不能为空",R.mipmap.ic_launcher);
                } else {
                    User_Message usermsg = daoHelper.getUsermsgByName(name);
                    if (usermsg == null) {
                        User_Message user = new User_Message();
                        user.setPassWord(password);
                        user.setName(name);
                        daoHelper.addUser(user);
                        showCustomToast("注册成功", R.mipmap.ic_launcher);
                        Intent intent = new Intent(this, LogInActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        showCustomToast("该用户已存在", R.mipmap.ic_launcher);
                    }
                }
                break;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.passwordET:
                noLookimage.setVisibility(View.VISIBLE);
                break;
            case R.id.nameET:
                noLookimage.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (count != 0) {
            registeredBtn.setBackgroundColor(0xffcecece);
            registeredBtn.setClickable(false);

        }else{
            registeredBtn.setBackgroundColor(0xff8BC12E);
            registeredBtn.setClickable(true);
        }
        temp = s;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        nameSizeTv.setText("还能输入" + (charMaxNum - s.length()) + "字符");
    }

    @Override
    public void afterTextChanged(Editable s) {
        editStart = nameET.getSelectionStart();
        editEnd = nameET.getSelectionEnd();
        if (temp.length() > charMaxNum) {
            Toast.makeText(getApplicationContext(), "你输入的字数已经超过了限制！", Toast.LENGTH_LONG).show();
            s.delete(editStart - 1, editEnd);
            int tempSelection = editStart;
            nameET.setText(s);
            nameET.setSelection(tempSelection);
        }

    }

    private void showCustomToast(String s, int image) {
        //获取LayoutInflater对象，该对象可以将布局文件转换成与之一致的view对象
        LayoutInflater inflater = getLayoutInflater();
        //将布局文件转换成相应的View对象
        View layout = inflater.inflate(R.layout.custome_toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        //从layout中按照id查找imageView对象
        ImageView imageView = (ImageView) layout.findViewById(R.id.ivForToast);
        //设置ImageView的图片
        imageView.setBackgroundResource(image);
        //从layout中按照id查找TextView对象
        TextView textView = (TextView) layout.findViewById(R.id.tvForToast);
        //设置TextView的text内容
        textView.setText(s);
        //实例化一个Toast对象
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 600);
        toast.setView(layout);
        toast.show();
    }
}
